package com.ccys.cms.controller.formulation;


import com.ccys.cms.service.formulation.FormulationService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.formulation.FormulationDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.entity.TblFormulation;
import com.ccys.db.mapper.TblFormulationMapper;
import com.ccys.db.vo.web.formulation.FormulationVo;
import com.ccys.db.vo.web.productAndService.IdNameVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/formulation")
@Api(tags = "配方管理")
public class FormulationController {


    @Resource
    private FormulationService formulationService;


    @Resource
    private TblFormulationMapper mapper;

    @PostMapping("formulation")
    @ApiOperation("新增")
    @Transactional
    public ResponseResult insert(TblFormulation model) {
        return formulationService.change(model);
    }


    @PutMapping("formulation")
    @ApiOperation("修改")
    @Transactional
    public ResponseResult update(TblFormulation model) {
        return formulationService.change(model);
    }





    @DeleteMapping("formulation")
    @ApiOperation("根据id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblFormulation model = new TblFormulation();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        return ResponseResult.update(formulationService.updateEntityById(model));
    }


    @GetMapping("formulation")
    @ApiOperation(value = "分页查询", notes = "排序字段  t1.name / t3.group_name / t1.disp_order / t1.status / t2.user_fullname / t1.created_on")
    public ResponseResult<PageInfo<FormulationVo>, Object> getPage(FormulationDto dto) {


        return formulationService.getPage(dto);
    }


    @GetMapping("formulation/list")
    @ApiOperation(value = "查询所有配方集合")
    public ResponseResult<List<IdNameVo>, Object> getList() {
        List<IdNameVo> list = mapper.getFormulationList();
        return ResponseResult.success(list);
    }
}
